//
//  FilterTransitionAnimator.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/12/15.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "FilterTransitionAnimator.h"

@implementation FilterTransitionAnimator

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    UIView *containerView = [transitionContext containerView];
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    // 从右侧扫入
    CGRect fromFrame    = [transitionContext initialFrameForViewController:fromVC];
    CGRect initialFrame = CGRectMake(CGRectGetWidth(fromFrame), 0, CGRectGetWidth(fromFrame) - 50, CGRectGetHeight(fromFrame));
    CGRect finalFrame   = CGRectMake(50, 0, CGRectGetWidth(fromFrame) - 50, CGRectGetHeight(fromFrame));
    
    // Presenting
    if (self.appearing) {
        fromView.userInteractionEnabled = NO;
        
        toView.frame = initialFrame;
        
        [containerView addSubview:toView];
        
        [UIView animateWithDuration:duration animations:^{
            toView.frame = finalFrame;
            fromView.alpha = 0.5;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    // Dismissing
    else {
        [UIView animateWithDuration:duration animations: ^{
            fromView.frame = initialFrame;
            toView.alpha = 1.0;
        } completion: ^(BOOL finished) {
            [fromView removeFromSuperview];
            toView.userInteractionEnabled = YES;
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
}

@end
